/**
 * 
 */
package co.gov.medellin.sdep.sisdep.admin.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.admin.exception.PersonaException;
import co.gov.medellin.sdep.sisdep.admin.exception.PersonaRuntimeException;
import co.gov.medellin.sdep.sisdep.admin.exception.descriptor.PersonaMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Persona;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IPersonaDAOT;

/**
 * @author Usuario
 *
 */
public class PersonaServiceImpl extends BaseService implements IPersonaService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IPersonaDAOT personaDAO;

	/**
	 * 
	 */
	public PersonaServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Persona persona) throws PersonaException {
		try{
			persona.setUsuarioCrea(getPrincipalUser());
			persona.setFechaCrea(new Date());
			getPersonaDAO().insert(persona);
		}catch (Exception e) {
			Object [] infoError = {
					persona.getIdentificacion()
			};
			if(e.getCause().toString().indexOf("ORA-00001") >= 0){
				throw new PersonaException(new PersonaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new PersonaException(new PersonaMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Persona persona) throws PersonaException {
		try{
			persona.setUsuarioModifica(getPrincipalUser());
			persona.setFechaModifica(new Date());
			getPersonaDAO().update(persona);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Persona persona) throws PersonaException {
		try {
			persona.setUsuarioModifica(getPrincipalUser());
			persona.setFechaModifica(new Date());
			getPersonaDAO().delete(persona);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Persona persona) throws PersonaException {
		try{
			//return getPersonaDAO().getAllPersonaPaginated(start, max, criteria);
			return getPersonaDAO().load(persona);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Persona get(long id) throws PersonaException {
		try{
			return getPersonaDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Persona findByRelated(Persona persona) throws PersonaException {
		try{
			return getPersonaDAO().findByCriteria(persona);
		}catch (Exception e) {
			Object [] infoError = {new Long(0)};
			throw new PersonaRuntimeException(new PersonaMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IPersonaDAOT getPersonaDAO() {
		return personaDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setPersonaDAO(IPersonaDAOT personaDAO) {
		this.personaDAO = personaDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
